import 'dart:math';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:getx_tutorial/app/modules/mine/getwidget/getwidget_controller.dart';

class GetWidgetPage extends GetView<GetWidgetController> {
// class GetWidgetPage extends GetWidget<GetWidgetController> {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('GetView')),

      body: SafeArea(
        child: Container(
          margin: EdgeInsets.all(20),
          child: ListView(
            children: [
              AttachWidget(
                controller: controller,
                title: "学习"
              ),
              Divider(),
              AttachWidget(
                controller: controller,
                title: "娱乐"
              ),
            ]
          )
        )
        
      )
    );
  }
}


class AttachWidget extends StatelessWidget {
  final GetWidgetController controller;
  final String title;

  AttachWidget({Key? key, required this.controller, this.title = '标题'}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    var rng = new Random();

    return Column(
      children: <Widget>[
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Text(
              this.title.toString(),
              style: Theme.of(context).textTheme.headline5
            ),
            ElevatedButton(
              onPressed: () {
                print('>>>> '+controller.hashCode.toString());
                controller.todos.add("Item - ${rng.nextInt(10000)}");
              },
              child: Text("Add"),
            ),
          ],
        ),
        
        Obx(() => ListView.builder(
          itemCount: controller.todos.length,
          shrinkWrap: true,
          itemBuilder: (ctx, int index) => Container(
            color: Colors.lightBlue.shade100,
            margin: EdgeInsets.symmetric(vertical: 5),
            padding: EdgeInsets.symmetric(horizontal: 5),
            child: Row(
              children: <Widget>[
                Expanded(
                  child: Text(controller.todos[index])
                ),
                IconButton(
                  icon: Icon(Icons.close),
                  onPressed: () => controller.todos.removeAt(index),
                ),
              ],
            ),
          )
          
        ),),
      ],
    );
  }
}